import 'package:bilibili/http/dao/home_dao.dart';
import 'package:bilibili/model/home_model.dart';
import 'package:bilibili/model/video_model.dart';
import 'package:bilibili/page/hi_base_tab_state.dart';
import 'package:bilibili/page/hi_nestedscrollview.dart';
import 'package:bilibili/widget/hi_banner.dart';
import 'package:bilibili/widget/video_card.dart';
import 'package:flutter/material.dart';

class HomeTabPage extends StatefulWidget {
  final String categoryName;
  final List<BannerMo>? bannerList;

  const HomeTabPage({Key? key, required this.categoryName, this.bannerList})
      : super(key: key);

  @override
  _HomeTabPageState createState() => _HomeTabPageState();
}

class _HomeTabPageState
    extends HiBaseTabState<HomeMo, VideoModel, HomeTabPage> {
  _banner(List<BannerMo> bannerList) {
    return HiBanner(
      bannerList,
      padding: EdgeInsets.only(left: 5, right: 5),
    );
  }

  @override
  // TODO: implement contentChild
  get contentChild => HiNestedScrollView(
        controller: scrollController,
        itemCount: dataList.length,
        padding: EdgeInsets.only(top: 10, left: 10, right: 10),
        headers: [
          if (widget.bannerList != null)
            Padding(
              padding: EdgeInsets.only(bottom: 8),
              child: _banner(widget.bannerList!),
            )
        ],
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, childAspectRatio: 0.82),
        itemBuilder: (BuildContext context, int index) {
          return VideoCard(videoModel: dataList[index]);
        },
      );

  @override
  Future<HomeMo> getData(int pageIndex) async {
    // TODO: implement getData
    HomeMo result = await HomeDao.get(widget.categoryName,
        pageIndex: pageIndex, pageSize: 10);
    return result;
  }

  @override
  List<VideoModel> parseList(HomeMo result) {
    // TODO: implement parseList
    return result.videoList;
  }
}
